const path = require("path")
//引入html相关插件
const HtmlWebpackPlugin = require('html-webpack-plugin');
//引入打包公共文件的插件
const CopyWebpackPlugin = require("copy-webpack-plugin")
module.exports = {
    //配置入口文件
    entry: path.resolve(__dirname,"../src/index.js"),
    //配置出口文件
    output: {
        path: path.resolve(__dirname,"../build"),
        //出口文件名称
        filename: "./js/main.js",
        //配置打包文件寻找资源的参照路径
        publicPath: "/"

    },

    //配置html-loader,用来处理html页面资源
    //配置ejs-loader,用来处理ejs页面资源
    module: {
        // rules: [
        //   {
        //     test: /\.html$/i,
        //     loader: "html-loader",
        //   },
        // ],
        rules: [
          {
            test: /\.ejs$/i,
            loader: "ejs-loader",
            //配置变量使ejs内部可以接收变量
            options:{
                variable: "data"
              }
          },
          //css-loader
          {
            test: /\.css$/i,
            use: ["style-loader", "css-loader"],
          },
        ],
      },
    
    resolve: {
        //配置路径别名
        alias: {
          $v: path.resolve(__dirname, "../src/views"),
          $r: path.resolve(__dirname, "../src/routers"),
          $a: path.resolve(__dirname, "../src/api")
        },
    
        //配置引入文件时 省略后缀,自动添加后缀的顺序
        extensions: [".js", ".json", ".ejs"]
      },

      // resolve: {
      //   //配置路径别名
      //   alias: {
      //     $v: path.resolve(__dirname, "../src/views")
      //   },
    
      //   //配置引入文件时 省略后缀,自动添加后缀的顺序
      //   extensions: [".js", ".json", ".ejs"]
      // },


    //插件,自动生成html,并将public文件复制到出口文件中,忽略其中的html文件
    plugins: [
        //设置自动生成html及其模块
        new HtmlWebpackPlugin({
            filename: "index.html",
            //绝对路径方便查找文件夹
            template: path.resolve(__dirname,"../public/index.html")
        }),new CopyWebpackPlugin({
            //复制public文件,忽略html
            patterns: [{
                from: path.resolve(__dirname,"../public"),
                to: "public",
                globOptions: {
                    ignore: ["**/index.html"]
                }
            }],
        }),
        
    ],
    mode: "production"
}